package com.jerry.common.utils;

import com.jerry.token.model.TokenModel;
import org.springframework.util.StringUtils;

import java.util.UUID;

/**
 * Filename:    com.jerry.common.utils.SecurityUtil
 * Description:
 * Copyright:   Copyright (c)2017
 * Company:     lngtop
 *
 * @author: 谢余锋
 * @version: 1.0
 * Create at:   2017/3/7 上午10:45
 * <p/>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2017/3/7      谢余锋        1.0        1.0 Version
 */
public class SecurityUtil {

    public static String bcrypt(String password) {
        return BCrypt.hashpw(password, BCrypt.gensalt(8));
    }

    public static boolean checkpw(String password, String hashed) {
        return BCrypt.checkpw(password, hashed);
    }

    public static String createTokenStr(TokenModel tokenModel){
        return EncryptUtil.encrypt(TokenModel.format(tokenModel));
    }

    public static TokenModel getToken(String tokenStr){
        if(StringUtils.isEmpty(tokenStr)){
            return null;
        }
        return TokenModel.parse(EncryptUtil.decrypt(tokenStr));
    }

    public static void main(String[] args){
        String pwd = "E10ADC3949BA59ABBE56E057F20F883E";
        //String token = createTokenStr(new TokenModel("aa","Jerry", UUID.randomUUID().toString().replaceAll("-","")));
        //System.out.println(token);
        //System.out.println(TokenModel.format(getToken("t9+EbpU45eBf7RJepRUbpEjKcK6XP/qR/dskFqOWGjmhUzrH98ivAYXcbHpFeceVG1Yy7LqR2uf6PpfmEmmwB7J94I47/c6S6qqI4LcGqSI=")));
        System.out.println(bcrypt(pwd));
    }
}
